package SuanfaFenxi;

import java.lang.reflect.Array;
import java.util.Arrays;

public class HomeWork_2 {
    public static int[] a = new int[20];
    public static void main(String[] args) {
        getCoin();
        for (int i = 0;i<20;i++){
            a[i] = (int)(Math.random()*50);
        }
        getSecondNum();

    }

    //16枚硬币找最小
    public static void getCoin(){
        int[] coin = new int[]{1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1};
        boolean flag = true;
        int num = 0;
        while(flag){
            int length = coin.length;
            int a = 0;
            int b = 0;
            if(length==1){
                System.out.println("最多为:"+num);
                break;
            }
            for(int i = 0;i<length/2;i++){
                a +=coin[i];
            }
            for(int i = length/2;i<length;i++){
                b +=coin[i];
            }
            if(a<b){
                coin = Arrays.copyOfRange(coin,0,coin.length/2);
            }else if(a>b){
                coin = Arrays.copyOfRange(coin,coin.length/2,coin.length);
            }
            num++;
        }
    }

    //找出n个元素里的第二大的数
    static int max;

    public static void getSecondNum(){
        System.out.println("打印原数组");
        for(int i :a){
            System.out.print(i+" ");
        }
        System.out.println();
        System.out.println("第二小值"+getMin(a.length));
    }

    public static int getMin(int n){
        if(n==2){
            if(a[n-2]>a[n-1]){
                max = a[n-2];
                return a[n-1];
            }else if(a[n-2]<=a[n-1]){
                max = a[n-1];
                return a[n-2];
            }
        }else{
            int min = getMin(n-1);
            if(a[n-1]>max){
                min = max;
                max = a[n-1];
            }
            if(a[n-1]<max&&a[n-1]>min){
                min = a[n-1];
            }
            return min;
        }
        return 0;
    }
}
